package com.dongdongshop.service.impl;

import com.dongdongshop.data.Result;
import com.dongdongshop.domain.model.TbContentCategory;
import com.dongdongshop.domain.model.TbContentCategoryExample;
import com.dongdongshop.domain.vo.ContentCategoryVO;
import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private TbContentCategoryMapper categoryMapper;

    @Override
    public List<ContentCategoryVO> contentList() {
        List<TbContentCategory> tbContentCategories = categoryMapper.selectByExample(null);
        return tbContentCategories.stream().map(tbContentCategory -> {
            ContentCategoryVO vo = new ContentCategoryVO();
            BeanUtils.copyProperties(tbContentCategory,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public Result addCategory(ContentCategoryVO vo) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(vo,category);
        categoryMapper.insertSelective(category);
        return Result.OK();
    }

    @Override
    public ContentCategoryVO selectById(Long id) {
        ContentCategoryVO vo = new ContentCategoryVO();
        TbContentCategory category = categoryMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(category,vo);
        return vo;
    }

    @Override
    public Result updateCategory(ContentCategoryVO vo) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(vo,category);
        categoryMapper.updateByPrimaryKeySelective(category);
        return Result.OK();
    }

    @Override
    public Result deleteCategory(List<Long> ids) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        example.createCriteria().andIdIn(ids);
        categoryMapper.deleteByExample(example);
        return Result.OK();
    }
}
